package com.csj.mvvmtest.ui

import android.Manifest
import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import com.csj.mvvm.ui.base.StartAct.onStart
import com.csj.mvvm.utils.*
import com.csj.mvvmtest.databinding.ActivitySplashBinding
import com.csj.mvvmtest.databinding.DialogAgreementLayoutBinding
import com.permissionx.guolindev.PermissionX


@SuppressLint("CustomSplashScreen")
class SplashActivity : AppCompatActivity() {
    lateinit var mContext: AppCompatActivity

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mContext = this
        if (Build.VERSION.SDK_INT >= 31) {
            installSplashScreen()
        }
        val binding = ActivitySplashBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initData()
    }

    fun initData() {
        if (!this.isTaskRoot) {
            val mainIntent = intent
            val action = mainIntent.action
            if (mainIntent.hasCategory(Intent.CATEGORY_LAUNCHER) && action == Intent.ACTION_MAIN) {
                finish()
                return
            }
        }
//        access()
        val spBoolean: Boolean by SPreference(SPreference.IS_AGREE_SLA, false)
        if (spBoolean) {
            access()
        } else {
            val bing: DialogAgreementLayoutBinding =
                DialogAgreementLayoutBinding.inflate(layoutInflater)
            val view: View = bing.root
            val mDialog: Dialog = CustomDialog.Builder(mContext).setView(view).setWrapLp()
                .setCancelable(false).create()
            mDialog.show()
            //取消
            bing.cancelTv.setOnClickListener {
                mDialog.dismiss()
                finish()
            }
            //同意
            bing.setTv.setOnClickListener {
                mDialog.dismiss()
                var spBoolean: Boolean by SPreference(SPreference.IS_AGREE_SLA, false)
                spBoolean = true
                access()
            }
            bing.tvSla.setOnClickListener {
                onStart<WebViewActivity>(mContext, Intent().putExtra("title", "服务协议"))
            }
            bing.tvPrivate.setOnClickListener {
                onStart<WebViewActivity>(mContext, Intent().putExtra("title", "隐私政策"))
            }
        }

    }

//    if (Build.VERSION.SDK_INT >= 23) {
//        if (Settings.canDrawOverlays(context)) {//Settings.System.canWrite()//Environment.isExternalStorageManager()
//            showFloatView()
//        } else {
//            val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)//Settings.ACTION_MANAGE_WRITE_SETTINGS//Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION
//            startActivity(intent)
//        }
//    } else {
//        showFloatView()
//    }

    private fun access() {
        PermissionX.init(mContext)
            .permissions(
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE
//                Manifest.permission.ACCESS_FINE_LOCATION,
//                Manifest.permission.ACCESS_COARSE_LOCATION,
//                Manifest.permission.ACCESS_BACKGROUND_LOCATION
//                Manifest.permission.SYSTEM_ALERT_WINDOW,
//                Manifest.permission.WRITE_SETTINGS,
//                Manifest.permission.CAMERA,
//                Manifest.permission.CALL_PHONE
            )
            .onExplainRequestReason { scope, deniedList ->
                scope.showRequestReasonDialog(deniedList, "请允许使用必要权限", "好的", "取消")
            }
            //Android11需要调用onForwardToSettings
            .onForwardToSettings { scope, deniedList ->
                scope.showForwardToSettingsDialog(deniedList, "请授予以下权限", "好的", "取消")
            }
            .request { allGranted, grantedList, deniedList ->
                LogUtil.e("allGranted:" + allGranted)
                if (allGranted) {
                    init()
                } else {
                    finish()
                }
            }
    }

    private fun isPkgInstalled(pkgName: String): Boolean {
        var packageInfo: PackageInfo?
        try {
            packageInfo = packageManager.getPackageInfo(pkgName, 0)
        } catch (e: PackageManager.NameNotFoundException) {
            packageInfo = null
            e.printStackTrace()
        }
        return packageInfo != null
    }

    private fun init() {
        SysUtils.initFiles()
        Handler(Looper.getMainLooper()).postDelayed({
            onStart<MainActivity>(mContext, intent)
            finish()
        }, 1000)

//        if (isPkgInstalled("com.taobao.taobao")) {
//            try {
//                val intent = Intent()
//                intent.action = "android.intent.action.VIEW"
//                intent.data = Uri.parse("https://detail.tmall.com/item.htm?id=642724192961")
//                //                intent.setData(Uri.parse("taobao://shop.m.taobao.com/shop/shop_index.htm?shop_id=57299736"));
//                intent.setClassName(
//                    "com.taobao.taobao",
//                    "com.taobao.tao.detail.activity.DetailActivity"
//                )
//                startActivity(intent)
//            } catch (e: Exception) {
//                e.printStackTrace()
//            }
//        }

//        val intent = Intent(Intent.ACTION_VIEW)
//        intent.data = Uri.parse("https://m.tb.cn/h.UrexsgI?tk=nxoLdQlLGaO")
//        startActivity(intent)

//        mContext.onStart<WebViewActivity>(
//            mContext,
//            Intent().putExtra("url", "https://m.tb.cn/h.UrexsgI?tk=nxoLdQlLGaO")
//        )


//        val intent = Intent(Intent.ACTION_VIEW);
//        intent.setData(Uri.parse("imeituan://www.meituan.com/shoppingCartFav"));//购物车
//        intent.setData(Uri.parse("imeituan://www.meituan.com/collection"));//收藏
//        intent.setData(Uri.parse("imeituan://www.meituan.com/search"));//搜索
//        intent.setData(Uri.parse("imeituan://www.meituan.com/phoenix/home/new"));//美团民宿
//        intent.setData(Uri.parse("imeituan://www.meituan.com/food/homepage"));//美食
//        intent.setData(Uri.parse("imeituan://www.meituan.com/legwork/homepage"));//跑腿
//        intent.setData(Uri.parse("imeituan://www.meituan.com/cab/internal/home"));//打车
//        intent.setData(Uri.parse("imeituan://www.meituan.com/mtmallhomepage"));//电商
//        intent.setData(Uri.parse("imeituan://www.meituan.com/hotel/homepage"));//酒店
//        intent.setData(Uri.parse("imeituan://www.meituan.com/mapchannel/travelmode"));//出行
//        intent.setData(Uri.parse("imeituan://www.meituan.com/bike/home"));//单车
//        intent.setData(Uri.parse("imeituan://www.meituan.com/traffic/homepage"));//汽车票
//        intent.setData(Uri.parse("imeituan://www.meituan.com/movie/mainpage"));//电影
//        intent.setData(Uri.parse("imeituan://www.meituan.com/joy/home"));//休闲  line2337

//        intent.setData(Uri.parse("imeituan://www.meituan.com/overseahotel/review?poi_id=93096499"));//评价
//        intent.setData(Uri.parse("imeituan://www.meituan.com/travel/poi?id=271772"));//门票detail
//        intent.setData(Uri.parse("imeituan://www.meituan.com//shareActivity"))
//        startActivity(intent);

//        val intent = Intent(Intent.ACTION_VIEW)
//        intent.data =
//            Uri.parse("http://dpurl.cn/fTzVT09z")
////            Uri.parse("https://h5.waimai.meituan.com/waimai/mindex/menu?mtShopId=1052044921710692&utm_source=5913&channel=appshare1")
//        startActivity(intent)

//        mContext.onStart<WebViewActivity>(
//            mContext,
//            Intent().putExtra("url","http://dpurl.cn/fTzVT09z")
////            Intent().putExtra("url","https://www.baidu.com/s?ie=UTF-8&wd=%E4%B8%AD%E9%80%9A%E5%BF%AB%E9%80%92%E6%9F%A5%E8%AF%A2")
//        )

    }

}